package com.kehongborui.fliggy.util.sqlite;

import com.kehongborui.fliggy.util.spider.HttpClient;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;

import static com.kehongborui.fliggy.util.sqlite.SqliteHelper.getInstance;

/**
 * @author zhang peng yang ming
 * @create 2018-09-13 23:18
 */
public class Main {
    public static String url = "http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=ssq&issueCount=100";
//    public static String url = "http://www.cwl.gov.cn";
    public void download() throws IOException {
        String s = HttpClient.get(url, "application/json", 20000, 20000);
        System.out.println(s);
    }

    public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
//        new Main().download();
        System.out.println(getJsonByInternet(url));
        SqliteHelper sqlite = getInstance("E:\\tmp\\ttt");
//        sqlite.executeUpdate("create table lot_color(" +
//                "id INTEGER PRIMARY KEY AUTOINCREMENT," +
//                "code CHAR(8)," +
//                "date CHAR(14)," +
//                "poolmoney INTEGER," +
//                "red1 CHAR(2)," +
//                "red2 CHAR(2)," +
//                "red3 CHAR(2)," +
//                "red4 CHAR(2)," +
//                "red5 CHAR(2)," +
//                "red6 CHAR(2)," +
//                "blue CHAR(2));");
//
//        sqlite.executeUpdate("INSERT INTO lot_color (poolmoney, code, date, red1, red2, red3, red4, red5, red6 ,blue) VALUES (427643568, '2018008', '2018-01-18(四)', '05','09','10','12','17','19','13')");
    }


    public static String getJsonByInternet(String path){
        try {
            URL url = new URL(path);
            HttpURLConnection httpurlConnection = (HttpURLConnection) url.openConnection();
            httpurlConnection.setRequestMethod("GET");
            httpurlConnection.setReadTimeout(8000);
            int responseCode = httpurlConnection.getResponseCode();
            if (responseCode == 200) {
                InputStream inputStream = httpurlConnection.getInputStream();
                StringBuilder stringBuilder = new StringBuilder();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String json = "";
                while ((json = bufferedReader.readLine()) != null) {
                    stringBuilder.append(json);
                }
                return stringBuilder.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
